package manager.analysis;

import static manager.utility.StaticFields.*;
import manager.data.RawDataObject;

public class SizeControl implements Validation {

	public boolean validate(RawDataObject dpo) {
		boolean ok=	packetSize(dpo.getDirection(),dpo.getSize(), dpo.getKey().length, dpo.getContent().length, dpo.getKeyInbound(), dpo.getParams()) &&
					fieldsSize(dpo.getTimestamp(), dpo.getSeqNumber(), dpo.getSession(), dpo.getUsername(), dpo.getPassword(), dpo.getOperation(), dpo.getWSName(), dpo.getWSNamespace(), dpo.getWSDLURL());
		//System.out.println("SizeControl: "+ok);
		return ok;
	}
	

	private boolean fieldsSize(long timestamp, long seqNumber, String session,
			String username, String password, String operation, String wsName,
			String wsNamespace, String wsdlurl) {
		if((timestamp>0) 				&&
			(seqNumber>=0)				&&
			(session.length()>0)		&&
			(username.length()>0)		&&
			(password.length()>0)		&&
			(operation.length()>0)		&&
			(wsName.length()>0)			&&
			(wsNamespace.length()>0)	&&
			(wsdlurl.length()>0))
			return true;
		return false;
	}


	private boolean packetSize(char direction, int size, int keySize, int contentSize, byte[] keyInbound, byte[] params){
		if((keySize!=KEY_SIZE) || (size!=keySize+contentSize))
			return false;

		int minSize=KEY_SIZE;
		if(direction == INBOUND)
			minSize+=CONTENT_MIN_SIZE;
		else
			minSize+=KEY_SIZE + CONTENT_MIN_SIZE;
		if(size<minSize)
			return false;

		if(keyInbound!=null)
			if(keyInbound.length!=KEY_SIZE)
				return false;

		if(params!=null)
			if((params.length>=contentSize) || params.length<=0)
				return false;

		return true;
	}
}
